#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<dos.h>
#include<alloc.h>

//cau truc "o vuong" gom 6 gia tri nguyen.
struct o_vuong{
    int x1,y1,x2,y2,n,m;
};
struct o_vuong **a;
int x_min, y_min, x_max, y_max,mouse_x,mouse_y;
int x,y,xi,yi,k=1,min,muc,tile1=30,tile2=tile1-3,dem;
char s[100],*p1,*p2;
unsigned size1,size2;
void doitoado(int mouse_x,int mouse_y, int &xi, int &yi);
int bit_get(int num, int pos);
void bit_set(int *num, int pos);
int right_up();
int left_up();
int initmouse();
void showmouse();
void hidemouse();
void getXY(int *mouseX, int *mouseY);
int left_down();
int right_down();
void setX(int start, int end);
void setY(int start, int end);
void Button(int x1, int y1, int x2, int y2, char t[50], int loai);
void giaodien(int muc);
void hieu_ung(int x1, int y1, int x2, int y2,int kieu,char s[100]);
o_vuong **tao_gia_tri(o_vuong **a);
void mucchoi(int &muc,int&x,int&y,int&min);
int gia_tri_null(int xi,int yi);
int thang_thua(int loai);

void main()
{
   int mh=0, mode=0,i,j,tam1=0;

   initgraph(&mh,&mode,"C:/TC/BGI");
   setbkcolor(2);
   setcolor(4);
   initmouse();
   showmouse();

   chon_muc: mucchoi(muc,x,y,min);
   tt: dem=(x-1)*(y-1)-min;//dem so o da dien dung.

   cleardevice();
   k=';';//khoi tao gia tri k de su dung trong vong lap - xoa bo dem.
   fflush(stdin);
   setbkcolor(2); //nen man hinh mau Green.
   setcolor(4);
   hidemouse();
   giaodien(muc); //ve giao dien voi muc choi tuong ung.
   showmouse();
   a=tao_gia_tri(a); //tao gia tri cho cac pha tu.
    /*for(int i=1;i<x;i++)
    {
        for(int j=1;j<y;j++) printf("%d",a[i][j].n);
        printf("\n");
    } */
    //sao chep anh qua min.
    size1=imagesize(510,345,537,372);
    p1=(char*)malloc(size1);
    //sao chep anh la co.
    size2=imagesize(510,315,537,342);
    p2=(char*)malloc(size2);
    k=1;
    //bat dau vong lap
    while(k!=27)//kiem tra co an ESC.
    {
        //kiem tra thang cuoc. neu thang thi k=26.
        if((dem==0)&&(k!=26))
        {
            Button(510,375,600,405,"Ban thang",0);
            k=26;
            delay(1500);
            tam1=2;
        }
        getXY(&mouse_x,&mouse_y);
        //kiem tra khi clcik chuot trai.
        if(left_down()!=0)
        {
            delay(200);
            hidemouse();

            doitoado(mouse_x,mouse_y,xi,yi);

            if(k!=26) //kiem tra thua cuoc. neu thua thi k=26.
            {
                gia_tri_null(xi,yi);
                /*
                if((a[xi][yi].m>0)&&(a[xi][yi].m<9))
                {
                    sprintf(s,"%d",a[xi][yi].n);
                    a[xi][yi].m=20;
                    hieu_ung(a[xi][yi].x1,a[xi][yi].y1,a[xi][yi].x2,a[xi][yi].y2,1,s);
                    dem--;
                    sprintf(s,"So o: %d",dem); //nut hien so o can tim.
                    Button(510,220,600,250,s,0);

                }
                else if(a[xi][yi].m==0)
                {
                    sprintf(s,"",1);
                    a[xi][yi].m=20;
                    hieu_ung(a[xi][yi].x1,a[xi][yi].y1,a[xi][yi].x2,a[xi][yi].y2,1,s);
                    dem--;
                    sprintf(s,"So o: %d",dem); //nut hien so o can tim.
                    Button(510,220,600,250,s,0);
                }*/
                if(a[xi][yi].m==9)
                {
                    for(i=1;i<x;i++)
                        for(j=1;j<y;j++){
                        if(a[i][j].n==9)

                        {
                            getimage(510,345,537,372,p1);
                            hieu_ung(a[i][j].x1-1,a[i][j].y1-1,a[i][j].x2+1,a[i][j].y2+1,2,"");
                            putimage(a[i][j].x1,a[i][j].y1,p1,0);
                        }
                        else if((a[i][j].m==30)&&(a[i][j].n!=9))
                        {
                            setcolor(4);
                            line(a[i][j].x1,a[i][j].y1,a[i][j].x2,a[i][j].y2);
                            line(a[i][j].x1,a[i][j].y2,a[i][j].x2,a[i][j].y1);
                        }
                        }
                    Button(510,375,600,405,"Ban thua",0);
                    delay(1500);
                    k=26;
                    //goto chon_muc1;
                    tam1=1;
                    /*sprintf(s,"*",1);
                    hieu_ung(b[xi][yi].x1,b[xi][yi].y1,b[xi][yi].x2,b[xi][yi].y2,1,s);
                    */
                }
            }
            //kiem tra co click vao nut thoat hay nut choi lai?
            if(a[xi][yi].n==11) goto tt; //neu vao nut thoat thi tro ve va tiep tuc.
            else if(a[xi][yi].n==12) exit(1); //neu vao nut thoat thi thoat.
            //printf("%d",a[xi][yi].n);
            showmouse();
        }
        //kiem tra khi click chuot phai.
        else if ((right_down()!=0)&&(k!=26))
        {
            delay(200);
            hidemouse();
            doitoado(mouse_x,mouse_y,xi,yi);
            if((a[xi][yi].m>=0)&&(a[xi][yi].m<=9))
            {
                a[xi][yi].m=30;
                //sprintf(s,"%c",6);
                getimage(510,315,537,342,p2);
                //hieu_ung(b[xi][yi].x1-1,b[xi][yi].y1-1,b[xi][yi].x2+1,b[xi][yi].y2+1,2,s);
                putimage(a[xi][yi].x1,a[xi][yi].y1,p2,0);


            }
            else if(a[xi][yi].m==30)
            {
                sprintf(s,"",1);
                a[xi][yi].m=a[xi][yi].n;
                hieu_ung(a[xi][yi].x1,a[xi][yi].y1,a[xi][yi].x2,a[xi][yi].y2,2,s);
            }
            showmouse();
            //printf("%d",a[xi][yi].n);

        }
        if(kbhit()) k=getch(); //An ESC de thoat.

        //kiem tra thang thua de tiep tuc choi
        int tam;
        showmouse();
        if(tam1==2)
        {
            tam1=0;
            tam=thang_thua(1);
            if(tam==1) goto tt;
            else if(tam==2) goto chon_muc;
            else if(tam==3) exit(1);
        }
        else if(tam1==1)
        {
            tam1=0;
            tam=thang_thua(0);
            if(tam==1) goto tt;
            else if(tam==2) goto chon_muc;
            else if(tam==3) exit(1);
        }



    }
    //chon_muc1: mucchoi(muc,x,y,min);


   getch();
   closegraph();
}

int bit_get(int num, int pos)
{
  return (num>>pos)&1;
}

void bit_set(int *num, int pos)
{
  *num|(1 << pos);
}
int initmouse()
{
  _AX = 0;
  geninterrupt(0x33);
  if (_AX == 0) return _AX; else return _BX;
}

void showmouse()
{
  _AX = 1;
  geninterrupt(0x33);
}

void hidemouse()
{
  _AX = 2;
  geninterrupt(0x33);
}

void getXY(int *mouseX, int *mouseY)
{
  _AX = 3;
  geninterrupt(0x33);
  *mouseX = _CX;
  *mouseY = _DX;
}

int left_down()
{
  _AX = 3;
  geninterrupt(0x33);
  return bit_get(_BX, 0);
}

int right_down()
{
  _AX = 3;
  geninterrupt(0x33);
  return bit_get(_BX, 1);
}
/*
void move(int mouseX, int mouseY)
{
  _AX = 4;
  _CX = mouseX;
  _DX = mouseY;
  geninterrupt(0x33);
}
*/
void setX(int start, int end)
{
  _AX = 7;
  _CX = start;
  _DX = end;
  geninterrupt(0x33);
}

void setY(int start, int end)
{
  _AX = 8;
  _CX = start;
  _DX = end;
  geninterrupt(0x33);
}

int left_up()
{
  int fx, fy, lx, ly;
  _BX = 0;
  _AX = 6;
  geninterrupt(0x33);
  if (_BX == 1)
  {
    fx = _CX; fy = _DX;
    getXY(&lx, &ly);
    if ((fx == lx)&(fy == ly)) return 1; else return 0;
  } else return 0;
}

int right_up()
{
  int fx, fy, lx, ly;
  _BX = 1;
  _AX = 6;
  geninterrupt(0x33);
  if (_BX == 1)
  {
    fx = _CX; fy = _DX;
    getXY(&lx, &ly);
    if ((fx == lx)&(fy == ly)) return 1; else return 0;
  } else return 0;
}

//Ve nut
void Button(int x1, int y1, int x2, int y2, char t[50], int loai)
{
    hidemouse(); //an chuot - khong hien chuot.
    int c1, c2;
    if (loai==0) {c1=15; c2=8;}
    if(loai==1) {c1=8; c2=15;}
    setcolor(c1); settextstyle(0,0,1);
    line(x1,y1,x2,y1);         //ngang 1
    line(x1,y1,x1,y2);            //doc 1
    setcolor(c2);
    line(x1+1,y2,x2,y2);        //ngang 2
    line(x2,y1+1,x2,y2);        //doc 2
    setcolor(7);setfillstyle(1,0);
    rectangle(x1+1,y1+1,x2-1,y2-1);
    setfillstyle(1,7);                    //Mau nut
    bar(x1+2,y1+2,x2-2,y2-2);
    setcolor(1); //settextstyle(2,0,1);
    settextjustify(1,1);
    outtextxy((x1+x2)/2,(y1+y2)/2+1,t);
    settextjustify(0,2);
    showmouse(); //hien thi chuot.
}


void doitoado(int mouse_x,int mouse_y, int &xi, int &yi)
{
    int i,j,k,l,x1,y1;
    xi=0; yi=2; //gan toa do khi an vao vung tro la o a[0][2].
    //doi toa do cho nut choi lai
    if((mouse_x>510)&&(mouse_x<600)&&(mouse_y>50)&&(mouse_y<80))
    {
        xi=0;yi=0;
    }
    //doi toa do cho nut thoat
    else if((mouse_x>510)&&(mouse_x<600)&&(mouse_y>260)&&(mouse_y<290))
    {
        xi=0;yi=1;
    }
    //doi toa do cho cac nut trong vung chinh.
    else
    {
        for(i=1;i<x;i++)
            for(j=1;j<y;j++)
            if((mouse_x>a[i][j].x1)&&(mouse_x<a[i][j].x2)&&(mouse_y>a[i][j].y1)&&(mouse_y<a[i][j].y2))
                {
                    xi=i;
                    yi=j;
                }
    }

    /*else
    {
        y1=1;
        for(i=x_min,j=i+tile2;i<x_max;i+=tile1,j=i+tile2,++y1)
        {
        x1=1;
        for(k=y_min,l=k+tile2;k<y_max;k+=tile1,l=k+tile2,++x1)
           if((mouse_x>i)&&(mouse_x<j)&&(mouse_y>k)&&(mouse_y<l))
           {
            b[x1][y1].x1=i;
            b[x1][y1].x2=j;
            b[x1][y1].y1=k;
            b[x1][y1].y2=l;
            xi=x1; yi=y1;
           }
        }
     }*/
}

//khoi tao giao dien tro choi.
void giaodien(int muc)
{
    //vung chu thich tro choi.
    setfillstyle(1,15);
    bar(490,310,620,410);

    //Bieu tuong la co trong tro choi.
    setlinestyle(0,0,3);
    Button(510,315,537,342,"",0);
    outtextxy(550,325,"Co");
    setcolor(4);
    setfillstyle(1,4);
    line(520,319,520,338);
    bar(520,319,530,327);

    //bieu tuong qua min trong tro choi.
    setlinestyle(0,0,2);
    Button(510,345,537,372,"",0);
    outtextxy(550,353,"Min");
    setcolor(4);
    setfillstyle(1,4);
    pieslice(524,359,0,360,5);
    line(524,349,524,368);
    line(515,359,533,359);
    line(517,366,531,352);
    line(517,352,531,366);

    //nut "dang choi" trong tro choi.
   Button(510,375,600,405,"Dang choi",0);

    //tieu de tro choi
   setcolor(4); //mau do
   setfillstyle(1,14); //kieu to net lien, nen mau vang
   bar(92,5,465,35); //khung tieu de.
   settextstyle(4,0,2); //font chu 4, co chu 2.
   outtextxy(150,6,"DO MIN PHIEN BAN 2.0");

   //khung man hinh chinh cua tro choi
   setlinestyle(0,0,3);
   setfillstyle(1,3);
   bar(92,42,465,415);
   rectangle(92,42,465,415);

   //thong tin ve nhom, dien dan vietsource.net
   setfillstyle(1,15);
   bar(92,425,466,470);
   settextstyle(0,0,1);
   outtextxy(200,427,"http://vietsource.net");
   outtextxy(160,442,"VSNET - Cong Dong Ma Nguon Viet");
   outtextxy(115,457,"Noi Chia Se Kien Thuc Lap Trinh & Ma Nguon");

   //thong tin tac gia NGUYEN VAN QUAN.
   settextstyle(0,1,2);
   bar(5,42,80,415);
   outtextxy(35,100,"NGUYEN VAN QUAN");
   settextstyle(0,1,1);
   outtextxy(55,120,"Skype: nguyenvanquan7826");
   outtextxy(70,90,"Email: nguyenvanquan7826@gmail.com");

   int i,j,k,l;

    //thong tin tro choi - cac nut dieu khien
    setfillstyle(1,15);
    bar(490,40,620,300);

    //cac duong ke den cac muc choi.
    setcolor(1);//cac duong mau xanh - muc khong duoc chon.
        line(515,130,595,130); //duong ngang tu muc 1 dem muc 3.
        line(515,130,515,140); //duong doc xuong muc 1.
        line(555,130,555,140); //duong doc xuong muc 2.
        line(595,130,595,140); //duong doc xuong muc 3.
    setcolor(4); //duong mau do tu "muc choi" xuong nga ba.
        line(555,120,555,130);

    //tao cac nut dieu khien va cac nut thong tin.
   Button(510,50,600,80,"Choi lai",0); //nut choi lai
   Button(510,90,600,120,"Muc choi",0); //nut hien thi muc choi
   Button(500,140,530,170,"1",0); //nut muc choi 1
   Button(540,140,570,170,"2",0); //nut muc choi 2
   Button(580,140,610,170,"3",0); //nut muc choi 3

   sprintf(s,"So min: %d",min); //nut hien so quan min.
   Button(510,180,600,210,s,0);

   sprintf(s,"So o: %d",dem); //nut hien so o can tim.
   Button(510,220,600,250,s,0);

   Button(510,260,600,290,"Thoat",0); //nut thoat khoi tro choi.

   //thiet lap cac thong so cho tung muc choi.
   if(muc==1)
    {
       setcolor(4);
       line(555,130,515,130); line(515,130,515,140); //duong ke den muc 1 neu dang choi
       Button(500,140,530,170,"1",1); //nut the hien muc 1 dang duoc choi

    }
   if(muc==2) //cac thong so tuong tu muc 1.
    {
       setcolor(4);
       line(555,130,555,140);
       Button(540,140,570,170,"2",1);
    }

   if(muc==3)//cac thong so tuong tu muc 1.
    {
       setcolor(4);
       line(555,130,595,130);line(595,130,595,140);
       Button(580,140,610,170,"3",1);
    }
    //getch();
    //ve cac o trong man hinh chinh cua tro choi.
    /*for(i=1;i<x;i++)
        for(j=1;j<y;j++)
            Button(a[i][j].x1,a[i][j].y1,a[i][j].x2,a[i][j].y2,"",0);*/

    for(i=x_min, j=i+tile2; i<x_max; i+=tile1, j=i+tile2)
        for(k=y_min, l=k+tile2; k<y_max; k+=tile1, l=k+tile2)
            Button(i,k,j,l,"",0);
}

//tao hieu ung khi an nut
void hieu_ung(int x1, int y1, int x2, int y2, int kieu, char s[100])
{
   //hieu ung kieu 1 - lom xuong.
   if(kieu==1)
   {
      Button(x1,y1,x2,y2,s,1);
      delay(200);
   }

   //hieu ung kieu 2 - lom xuong roi nay len.
   if(kieu==2)
   {
      Button(x1,y1,x2,y2,s,1);
      delay(200);
      Button(x1,y1,x2,y2,s,0);
   }

}

o_vuong **tao_gia_tri(o_vuong **a)
{
    int i,j,k;
    
    //cap phat bo nho va tao gia tri dau cho cac o (gia tri ban dau =0).
    a=new o_vuong*[x+2];
    for(i=0;i<=x+2;i++)
    {
        a[i]=new o_vuong [y+2];
        for(j=0;j<=y+2;j++)
        {
            a[i][j].n=0;
            a[i][j].m=0;
            a[i][j].x1=0;
            a[i][j].y1=0;
            a[i][j].x2=0;
            a[i][j].y2=0;
        }
    }

    //khoi tao min voi gia tri ngau nhien (min co gia tri la 9).
    randomize();
    for(k=1;k<=min;)
    {
        i=random(x-1)+1;
        j=random(y-1)+1;
        if(a[i][j].n!=9)
        {
            k++;
            a[i][j].n=9;
        }
    }

    //gan toa do cho cac nut/cac o.
    a[1][1].x1=x_min;
    a[1][1].y1=y_min;
    a[1][1].x2=x_min+tile2;
    a[1][1].y2=y_min+tile2;
    for(j=1;j<y;j++)
    for(i=1;i<x;i++)
    {
        a[i][j].x1=a[1][1].x1+(i-1)*tile1;
        a[i][j].x2=a[i][j].x1+tile2;
        a[i][j].y1=a[1][1].y1+(j-1)*tile1;
        a[i][j].y2=a[i][j].y1+tile2;
    }

    //tao gia tri cho cac o.
    for(i=1;i<x;i++)
        for(j=1;j<y;j++)
            if(a[i][j].n!=9)
                {
                    a[i][j].n=0;
                    if(a[i-1][j].n==9) a[i][j].n++;
                    if(a[i-1][j+1].n==9) a[i][j].n++;
                    if(a[i][j+1].n==9) a[i][j].n++;
                    if(a[i+1][j+1].n==9) a[i][j].n++;
                    if(a[i+1][j].n==9) a[i][j].n++;
                    if(a[i+1][j-1].n==9) a[i][j].n++;
                    if(a[i][j-1].n==9) a[i][j].n++;
                    if(a[i-1][j-1].n==9) a[i][j].n++;
                }
    a[0][2].n=20; //khi an vao vung trong thi co gia tri 20
    a[0][0].n=11; //nut choi lai co gia tri la 11
    a[0][1].n=12; //Nut thoat co gia tri la 12

    //gan gia tri n cho gia tri n.
    for(i=0;i<=x+2;i++)
        for(j=0;j<=y+2;j++)
            a[i][j].m=a[i][j].n;


    return a;
}
void mucchoi(int &muc,int &x, int&y,int&min)
{
   hidemouse();
   cleardevice();
   settextstyle(0,0,2);
   outtextxy(150,60,"CHAO MUNG BAN DEN VOI");
   outtextxy(155,100,"DO MIN PHIEN BAN 2.0");

   settextstyle(0,0,1);
   setcolor(1);
   outtextxy(230,180,"Moi ban chon muc choi");
   Button(270,200,360,230,"Muc choi",0);
   Button(260,250,290,280,"1",0);
   Button(300,250,330,280,"2",0);
   Button(340,250,370,280,"3",0);
   showmouse();
   muc=0;
   while(muc==0)
   {
        getXY(&mouse_x,&mouse_y);
        if(left_down()!=0)
        {
            delay(200);
            if((mouse_x>260)&&(mouse_x<290)&&(mouse_y>250)&&(mouse_y<280))
                {muc=1;k=27;x=7;y=7;min=5;}
            if((mouse_x>300)&&(mouse_x<330)&&(mouse_y>250)&&(mouse_y<280))
                {muc=2;k=27;x=10;y=10;min=10;}
            if((mouse_x>340)&&(mouse_x<370)&&(mouse_y>250)&&(mouse_y<280))
                {muc=3;k=27;x=13;y=13;min=15;}
        }
   }
    if(muc==1)//toa do dau, cuoi cua man hinh muc 1
    {
        x_min=190;
        y_min=140;
        x_max=370;
        y_max=320;
    }
    if(muc==2) //cac thong so tuong tu muc 1.
    {
       x_min=145;
       y_min=95;
       x_max=415;
       y_max=365;
    }

    if(muc==3)//cac thong so tuong tu muc 1.
    {
       x_min=100;
       y_min=50;
       x_max=460;
       y_max=410;
    }
}
int gia_tri_null(int xi,int yi)
{
   int xj=xi,yj=yi;
   if(a[xj][yj].m==0)
   {
       a[xj][yj].m=20;
       sprintf(s,"",1);
       hieu_ung(a[xj][yj].x1,a[xj][yj].y1,a[xj][yj].x2,a[xj][yj].y2,1,s);
       dem--;
       sprintf(s,"So o: %d",dem); //nut hien so o can tim.
       Button(510,220,600,250,s,0);
       if(yj>1) gia_tri_null(xj,yj-1);
       xj=xi;yj=yi;
       if(xj<x-1) gia_tri_null(xj+1,yj);
       xj=xi;yj=yi;
       if(yj<y-1) gia_tri_null(xj,yj+1);
       xj=xi;yj=yi;
       if(xj>1) gia_tri_null(xj-1,yj);
       xj=xi;yj=yi;
       if((xj>1)&&(yj>1)) gia_tri_null(xj-1,yj-1);
       xj=xi;yj=yi;
       if((xj<x-1)&&(yj<y-1)) gia_tri_null(xj+1,yj+1);
       xj=xi;yj=yi;
       if((xj>1)&&(yj<y-1)) gia_tri_null(xj-1,yj+1);
       xj=xi;yj=yi;
       if((xj<x-1)&&(yj>1)) gia_tri_null(xj+1,yj-1);
   }
   else if((a[xj][yj].m>0)&&(a[xj][yj].m<9))
    {
       sprintf(s,"%d",a[xj][yj].n);
       a[xj][yj].m=20;
       hieu_ung(a[xj][yj].x1,a[xj][yj].y1,a[xj][yj].x2,a[xj][yj].y2,1,s);
       dem--;
       sprintf(s,"So o: %d",dem); //nut hien so o can tim.
       Button(510,220,600,250,s,0);
       return 0;
    }
    else return 0;
    return 0;
}
int thang_thua(int loai)
{
    hidemouse();
    cleardevice();
    setfillstyle(1,13);
    bar(100,140,458,320);
    int i=103, j=250;
    Button(i+20,j,i+110,j+30,"Choi lai",0);
    Button(i+130,j,i+220,j+30,"Chon muc",0);
    Button(i+240,j,i+330,j+30,"Thoat",0);
    if(loai==0)
    {
        setcolor(4);
        outtextxy(i+100,j-50,"Rat tiec, ban da thua!");
    }
    else if(loai==1)
    {
        setcolor(4);
        outtextxy(i+80,j-50,"Chuc mung, ban da thang!");
    }
    showmouse();
    while(1)
    {
        getXY(&mouse_x,&mouse_y);
        if(left_down()!=0)
        {
            delay(200);   //i+20,j,i+110,j+30  nut choi lai
            if((mouse_x>i+20)&&(mouse_x<i+110)&&(mouse_y>j)&&(mouse_y<j+30))
                return 1;
            //i+240,j,i+330,j+30 //nut thoat
            if((mouse_x>i+240)&&(mouse_x<i+330)&&(mouse_y>j)&&(mouse_y<j+30))
                return 3;
            //i+130,j,i+220,j+30
            if((mouse_x>i+130)&&(mouse_x<i+220)&&(mouse_y>j)&&(mouse_y<j+30))
                return 2;
        }
    }
    //getch();

}